import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ViewUI from 'view-design'
import axios from 'axios'
import '@/assets/plugin/animate.css'
import 'view-design/dist/styles/iview.css'
import '@/assets/scss/main.scss'
import '@/assets/fonts/iconfont/iconfont.css'
import '@/assets/fonts/iconfont/iconfont.js'

import { Message } from 'view-design';

axios.defaults.baseURL = 'http://115.29.191.178/'
Vue.prototype.axios = axios
Vue.config.productionTip = false
Vue.use(ViewUI);
Vue.component('Message',Message);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

router.beforeEach((to, from, next) => {
  ViewUI.LoadingBar.start();

  const token = store.state.token !== null ?
    store.state.token :
    window.sessionStorage.getItem('token');

  const verified = store.state.verified !== 0 ?
    store.state.verified :
    window.sessionStorage.getItem('verified');

    if (!to.meta.requireAuth) next();
    else if (token == null) {
      // 未登录
      next({
        name: 'login'
      });
      Message.warning('请先登录后再进行操作');
      ViewUI.LoadingBar.finish();
      return;
    }
    
    if (to.meta.requireSuperAuth && verified == 0) {
      next({
        name: 'verify'
      });
      Message.warning('请先认证后再进行操作');
      ViewUI.LoadingBar.finish();
    }
    
    next();
});

router.afterEach(route => {
  ViewUI.LoadingBar.finish();
});